package com.cmwa.scbp.business.infodisclosure.investReport.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.scbp.base.mvc.entity.Employee;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.business.infodisclosure.investReport.dao.InvestReportDao;
import com.cmwa.scbp.business.infodisclosure.investReport.entity.InvestReportVo;
import com.cmwa.scbp.business.infodisclosure.investReport.service.InvestReportService;
import com.cmwa.scbp.sys.attach.dao.AttachDao;
import com.cmwa.scbp.sys.attach.entity.AttachVo;
import com.cmwa.scbp.sys.attach.service.AttachService;
import com.cmwa.scbp.utils.SessionUtils;
import com.cmwa.scbp.utils.cached.DictionaryCached;

/**
 * 投资者报告表业务实现层
 * 
 * @author ex-zhengqq
 * @date 2019-01-23 13:37:22
 */
@Service
public class InvestReportServiceImpl implements InvestReportService {
	@Resource
	private InvestReportDao investReportDao;
	
	@Resource
	private AttachDao attachDao;
	@Resource
	private AttachService attachService;
	
	@Override
	public void insertInvestReport(InvestReportVo investReport) {
		String empId = SessionUtils.getEmployee().getID();
		investReport.setCreateId(empId);
		investReport.setStatus("S");
		investReportDao.insertInvestReport(investReport);
	}
	
	@Override
	public void physicDeleteInvestReportById(String id) {
		investReportDao.physicDeleteInvestReportById(id);
	}
	
	@Override
	public void logicDeleteInvestReportById(String id) {
		investReportDao.logicDeleteInvestReportById(id);
	}
	
	@Override
	public void updateInvestReportById(InvestReportVo investReport) {
		investReportDao.updateInvestReportById(investReport);
	}
	
	@Override
	public InvestReportVo getInvestReportById(String investRepId) {
		return investReportDao.getInvestReportById(investRepId);
	}
	
	@Override
	public List<InvestReportVo> getInvestDataAttachById(String ids) {
		return investReportDao.getInvestDataAttachById(ids.split(","));
	}
	
	@Override
	public List<InvestReportVo> getInvestFileAttachById(String ids) {
		return investReportDao.getInvestFileAttachById(ids.split(","));
	}
	
	@Override
	public List<AttachVo> getInvestAttach(String investRepId,String investUploadType){
		return investReportDao.getInvestAttach(investRepId,investUploadType);
	}
	
	@Override
	public Page<InvestReportVo> investReportListPage(SearchParam sp) {
		Employee employee = SessionUtils.getEmployee();
		if (StringUtils.equals(employee.getEmpType(), "OUT")) {
			sp.getSp().put("empId", employee.getID());
		}
		List<InvestReportVo> items = investReportDao.investReportListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}
	
	@Override
	public void addAttachRel(String attIdStr,String investUploadType, String investRepId){
		String empId = SessionUtils.getEmployee().getID();
		String[] attIdStrs = attIdStr.split(",");
		String attIds = "";
		for(String attId : attIdStrs){
			AttachVo attachVo =attachDao.findAttachAndRelByAttId(attId);
			if(null == attachVo){
				attIds += attId + ",";
			}
		}
		if(StringUtils.isNotBlank(attIds)){
			//"INVEST_REPORT"
			attachDao.addAttachRel2(empId, investRepId, investUploadType, attIds);
		}
	}
	
	@Override
	public List<Map<String, String>> countReportData(SearchParam param){
		Employee employee = SessionUtils.getEmployee();
		if (StringUtils.equals(employee.getEmpType(), "OUT")) {
			param.getSp().put("empId", employee.getID());
		}
		return investReportDao.countReportData(param);
	}
	
	@Override
	public List<Map<String, String>> countReportSize(SearchParam param){
		Employee employee = SessionUtils.getEmployee();
		if (StringUtils.equals(employee.getEmpType(), "OUT")) {
			param.getSp().put("empId", employee.getID());
		}
		return investReportDao.countReportSize(param);
	}
	
	public List<InvestReportVo> getInvestByType(String reportType,String reprotDateBgn,String reprotDateEnd){
		String empId = "";
		Employee employee = SessionUtils.getEmployee();
		if (StringUtils.equals(employee.getEmpType(), "OUT")) {
			empId = employee.getID();
		}
		
		return investReportDao.getInvestByType(reportType, reprotDateBgn, reprotDateEnd, empId);
	}
	
	public void downloadAttachZip(String investRepIds,String investUploadType,String downType,HttpServletResponse response,HttpServletRequest request) throws IOException{
		//判断前端传递过来的业务id是否为空
		if(StringUtils.isNotBlank(investRepIds)){
			//根据业务id查询出来的相关附件
			List<InvestReportVo> investRepList = null;
			String fileName = "投资者报告";
			if(StringUtils.equalsIgnoreCase("INVEST_DATA", investUploadType)){
				investRepList = getInvestDataAttachById(investRepIds);
				fileName += "-材料";
			}else if(StringUtils.equalsIgnoreCase("INVEST_FILE", investUploadType)){
				investRepList = getInvestFileAttachById(investRepIds);
				fileName += "-报告";
			}
			
			if(StringUtils.equalsIgnoreCase("SINGLE", downType) && CollectionUtils.isNotEmpty(investRepList)){
				fileName += "-" + investRepList.get(0).getPrjsname();
			}
			
			//根据产品将附件分类组装
			Map<String,String> map = new HashMap<String,String>();
			if(CollectionUtils.isNotEmpty(investRepList)) {
				
				if(StringUtils.equalsIgnoreCase("SINGLE", downType)){//单个下载
					//调用公共批量下载方法
					attachService.downloadAttachZip(investRepList.get(0).getAttIds(), fileName, request, response);
				}else if(StringUtils.equalsIgnoreCase("BATCH", downType)){//批量下载
					for(InvestReportVo investReportVo : investRepList){
						if(StringUtils.isNotEmpty(investReportVo.getAttIds())){
							map.put(investReportVo.getPrjsname(), investReportVo.getAttIds());
						}
					}
					//调用公共批量下载方法
					attachService.downloadAttachZip(map, fileName, request, response);
				}
			}else{
				//String fileName = DictionaryCached.getDictName("SERVICE_TYPE", "INFO_DIS_INVESTER");
				attachService.downloadAttachZip("", fileName, request, response);
			}
		}else{
			String fileName = DictionaryCached.getDictName("SERVICE_TYPE", "INFO_DIS_INVESTER");
			attachService.downloadAttachZip("", fileName, request, response);
		}
	}
}
